<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class IC extends Model
{
    protected $table = 'ic';

    protected $primaryKey = 'ic_id';

    protected $keyType = 'string';

    protected $fillable = [
    	'print_code',
    	'ic_id',
    	'ic_status',
    	'process_number',
    	'current_process_node',
    	'next_process_node',
    	'vehicle_number',
    	'driver_name',
    	'driver_idcard',
    	'driver_mobile_phone',
    	'update_datetime',
        'order_systemnumber',
    	'remarks'
    ];

    public $timestamps = false;

    const PROCESS_NODE_ZERO = '0';

    const PROCESS_NODE_ONE = '1';

    const PROCESS_NODE_TWO = '2';

    const PROCESS_NODE_THREE = '3';

    public static $PROCESS_NODE = [
    	self::PROCESS_NODE_ZERO => '出厂',
    	self::PROCESS_NODE_ONE => '制卡',
    	self::PROCESS_NODE_TWO => '进厂',
        self::PROCESS_NODE_THREE => '已装（卸）货 '
    ];

    const PROCESS_NUMBER_SALE = 'SALE';

    const PROCESS_NUMBER_PURCHASE = 'PURCHASE';

    public static $PROCESS_NUMBER = [
        self::PROCESS_NUMBER_SALE => '提货',
        self::PROCESS_NUMBER_PURCHASE => '送货'
    ];

    const IC_STATUS_VLAID = 'VALID';

    const IC_STATUS_NOVLAID = 'NOVALID';

    public static $IC_STATUS = [
        self::IC_STATUS_VLAID => '有效',
        self::IC_STATUS_NOVLAID => '无效'
    ];

    protected $appends = [
        'process_number_desc',
        'ic_status_desc',
    	'current_process_node_desc',
    	'next_process_node_desc'
    ];

    public function getProcessNumberDescAttribute(){
        if(array_key_exists('process_number', $this->attributes) && $this->attributes['process_number'] != null){
            return $this->attributes['process_number_desc'] = self::$PROCESS_NUMBER[$this->attributes['process_number']];
        }
    }

    public function getIcStatusDescAttribute(){
        if(array_key_exists('ic_status', $this->attributes) && $this->attributes['ic_status'] != null){
            return $this->attributes['ic_status_desc'] = self::$IC_STATUS[$this->attributes['ic_status']]; 
        }
    }

    public function getCurrentProcessNodeDescAttribute(){
    	if(array_key_exists('current_process_node',$this->attributes) && $this->attributes['current_process_node'] != null){
            return $this->attributes['current_process_node_desc'] = self::$PROCESS_NODE[$this->attributes['current_process_node']];
        }
    }

    public function getNextProcessNodeDescAttribute(){
    	if(array_key_exists('next_process_node',$this->attributes) && $this->attributes['next_process_node'] != null){
            return $this->attributes['next_process_node_desc'] = self::$PROCESS_NODE[$this->attributes['next_process_node']];
        }
    }

    public function icRecord(){
        return $this->hasOne('\App\Models\IcRecord','order_systemnumber','order_systemnumber');
    }


    public function weightdata(){
        return $this->hasOne('\App\Models\Weighdata','order_systemnumber','order_systemnumber');
    }

    public function weightAbnormal(){
        return $this->hasOne('\App\Models\WeighAbnormal','order_systemnumber','order_systemnumber');
    }

    public function flowextemal(){
        return $this->hasOne('\App\Models\FlowExtemal','order_systemnumber','order_systemnumber');
    }
}
